<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Auth extends CI_Controller {


	function register() {
		
		$this->load->model('auth_model');
		
		$this->load->library('form_validation');
		$this->form_validation->set_rules('name', 'Meno', 'trim|required|xss_clean|min_length[3]');
		$this->form_validation->set_rules('mail', 'mailová adresa', 'trim|required|valid_mail');
		$this->form_validation->set_rules('passwd', 'Heslo', '|trim|required|min_length[4]');
		$this->form_validation->set_error_delimiters('<p class = "valid_error">', '</p>');
		
		if ($this->form_validation->run() && $this->auth_model->register()) {
			
			redirect('projekt');
		
		}
		else {
			// napis hlasku o chyba
		}
		
		$this->load->view('register_view');

	}

	function testing() {
		echo 'testing funct';
	}	
	
	function login() {
		$testik['chybova'] = '';
		$this->load->model('auth_model');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('mail', 'mailová adresa', 'trim|required');
		$this->form_validation->set_rules('passwd', '|trim|required');
		$this->form_validation->set_error_delimiters('<p class = "valid_error">', '</p>');
		if ($this->form_validation->run()) {
		
			if ($this->auth_model->check()) {
				$data = $this->auth_model->getUserData($_POST['mail']);
				$data['logged_in'] = true;
				$this->session->set_userdata($data); // tuto nastavi tie svoje session array data
				
				redirect('projekt/logged');
			
			}
			else {
			
				$testik['chybova'] = 'mail alebo heslo je chybné.';
			}		
		}
		else {
			$testik['chybova'] = 'Stala sa chyba, skúste znova';
			redirect('projekt/testing');
		}
		
		
	}

	function logout() {
		
		$this->session->unset_userdata(array('id'=>'', 'name'=>'', 'mail'=>'','logged_in'=>''));
		redirect('projekt');
	
		
	}


	
			

}